<?php


namespace app\admin\controller;


use app\admin\service\RbacService;
use app\common\controller\CommonController;
use app\common\exception\ParameterFailedException;
use app\common\exception\NotFoundException;
use app\common\model\ArticleModel;
use app\common\model\BannerModel;
use app\common\model\MediaModel;
use app\common\model\ProductCategoryModel;
use app\common\model\ProductModel;
use app\common\service\Lang;
use app\common\validate\BannerValidate;
use think\facade\Db;
use think\facade\Request;

class BannerController extends CommonController
{
    public function getBannerList(Request $request)
    {
        $models = (new BannerModel())->getBannerListPaginate([], $this->page, $this->limit);
        return retJson(Lang::GETDone, $models->toArray());
    }

    public function postBanner(Request $request)
    {
        $params      = self::getParam([
            'media_url',
            'event',
            'event_id',
            'is_carousel',
        ]);
        (new BannerValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $oldModel = (new BannerModel())->where('event', $params['event'])
                ->where('event_id', $params['event_id'])
                ->find();
            if ($oldModel) {
                throw new ParameterFailedException(['msg' => '已存在该数据']);
            }

            switch ($params['event']) {
                case 'category' :
                    $categoryModel = (new ProductCategoryModel())->where('id', $params['event_id'])->find();
                    if (!$categoryModel) {
                        throw new ParameterFailedException(['msg' => '分类不存在']);
                    }
                    break;
                case 'product' :
                    $productModel = (new ProductModel())->where('id', $params['event_id'])->find();
                    if (!$productModel) {
                        throw new ParameterFailedException(['msg' => '商品不存在']);
                    }
                    break;
                case 'article' :
                    $articleModel = (new ArticleModel())->where('id', $params['event_id'])->find();
                    if (!$articleModel) {
                        throw new ParameterFailedException(['msg' => '文章不存在']);
                    }
                    break;
                default:
                    throw new ParameterFailedException(['msg' => '事件错误']);
            }

            $model              = new BannerModel();
            $model->media_url   = $params['media_url'];
            $model->event       = $params['event'];
            $model->event_id    = $params['event_id'];
            $model->is_carousel = $params['is_carousel'];

            $model->save();

            Db::commit();
            return retJson(Lang::POSTDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function putBanner(): ?\think\response\Json
    {
        $params      = self::getParam([
            'id',
            'media_url',
            'event',
            'event_id',
            'is_carousel',
        ]);
        (new BannerValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = BannerModel::where('id', $params['id'])->find();
            if (!$model) {
                throw new NotFoundException();
            }

            switch ($params['event']) {
                case 'category' :
                    $categoryModel = (new ProductCategoryModel())->where('id', $params['event_id'])->find();
                    if (!$categoryModel) {
                        throw new ParameterFailedException(['msg' => '分类不存在']);
                    }
                    break;
                case 'product' :
                    $productModel = (new ProductModel())->where('id', $params['event_id'])->find();
                    if (!$productModel) {
                        throw new ParameterFailedException(['msg' => '商品不存在']);
                    }
                    break;
                case 'article' :
                    $articleModel = (new ArticleModel())->where('id', $params['event_id'])->find();
                    if (!$articleModel) {
                        throw new ParameterFailedException(['msg' => '文章不存在']);
                    }
                    break;
                default:
                    throw new ParameterFailedException(['msg' => '事件错误']);
            }

            $model->media_url   = $params['media_url'];
            $model->event       = $params['event'];
            $model->event_id    = $params['event_id'];
            $model->is_carousel = $params['is_carousel'];
            $model->save();

            Db::commit();
            return retJson(Lang::PUTDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }

    public function deleteBanner()
    {
        $params      = self::getParam(['id']);
        (new BannerValidate())->goCheck(__FUNCTION__, $params);
        Db::startTrans();
        try {
            $model = BannerModel::where('id', $params['id'])->find();
            if (!$model) {
                throw new NotFoundException();
            }

            $model->delete_time = NOW_TIME;
            $model->save();

            Db::commit();

            return retJson(Lang::DELETEDone);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            throw $e;
        }
    }
}